﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MMOShared;
using System.IO;
using Microsoft.Build.BuildEngine;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;

namespace MMOResCreator
{
    public partial class CompileContentForm : Form
    {
        private bool itemToCompile;
        private bool projectPathSelected;
        private ContentCompiler contentComp;

        
        public CompileContentForm(List<ResourceEntry> files,UInt16 versionToCompile)
        {
            InitializeComponent();

            //default
            itemToCompile = false;
            projectPathSelected = false;
        
            //Disable compile button
            this.compileB.Enabled = false;

            //Check for items
            if (files.Count > 0)
                this.itemToCompile = true;

            contentComp = new ContentCompiler(versionToCompile,projPathL.Text);
            contentComp.SetEntriesToCompile(files);
            FillFileList();
        }

        private void FillFileList()
        {
            foreach (CompileResourceEntry c in contentComp.CompilationFiles)
            {
                this.compFilesLV.Items.Add(new ListViewItem(c.getAsViewItem()));
            }
        }

        public void CheckCompileButtonState()
        {
            if (itemToCompile && projectPathSelected)
                this.compileB.Enabled = true;
        }

        private void projPathB_Click(object sender, EventArgs e)
        {    
            SaveFileDialog o = new SaveFileDialog();
            o.Filter = "Project Files (*.csproj)|*.csproj";
            o.DefaultExt = ".csproj";
            o.AddExtension = true;
            if (o.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                projectPathSelected = true;
                string fullPath = o.FileName;
                contentComp.SetProjectPath(fullPath);
                projPathL.Text = fullPath;
            }
            CheckCompileButtonState();
        }

        private void compileB_Click(object sender, EventArgs e)
        {
            contentComp.Compile();
        }
    }
}
